package com.by.yz.loginUtil;

import android.util.Log;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.JsonObjectRequest;
import com.by.yz.App;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.net.CookieStore;
import java.net.HttpCookie;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by mrs yang on 2016/10/22.
 */
public class UserRESTFul {
    public interface UserGETCallBack{
        public void ok(JSONArray response) throws JSONException;
        public void error(String response);
    }
    public interface UserPOSTCallBack{
        public void ok(JSONObject response) throws JSONException;
        public void error(String response);
    }
    public void getAllUser(final UserGETCallBack cb){
        String url=Constants.server_url+"/api/userinfos";
        JsonArrayRequest getRequest=new JsonArrayRequest(Request.Method.GET, url, null, new Response.Listener<JSONArray>() {
            @Override
            public void onResponse(JSONArray response) {
                try {
                    cb.ok(response);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        error.printStackTrace();
                        cb.error(error.toString());
                    }
                }){

            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String,String> headers=new HashMap<String,String>();
                CookieStore cookieJar= App.cookieManager.getCookieStore();
                List<HttpCookie> cookies=cookieJar.getCookies();
                for (HttpCookie cookie:cookies){
                    if(cookie.toString().contains("XSRF-TOKE")){
                        Log.e("User1-token-post",cookie.getValue());
                        headers.put("X-XSRF-TOKEN",cookie.getValue());
                    }
                }
                return headers;
            }
        };
        App.requestQueue.add(getRequest);
    }
    public void postUser(JSONObject data,final UserPOSTCallBack cb){
        String url=Constants.server_url+"/api/userinfos";
        JsonObjectRequest getRequst=new JsonObjectRequest(Request.Method.POST, url, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    cb.ok(response);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        error.printStackTrace();
                        cb.error( error.toString());
                    }
                }){

            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String,String> headers=new HashMap<String,String>();
                CookieStore cookieJar=App.cookieManager.getCookieStore();
                List<HttpCookie> cookies=cookieJar.getCookies();
                for(HttpCookie cookie:cookies){
                    if(cookie.toString().contains("XSRF-TOKE")){
                        Log.e("User2-token-post",cookie.getValue());
                        headers.put("X-XSRF-TOKEN",cookie.getValue());
                    }
                }
                headers.put("Content-Type", "application/json; charset=utf-8");
                return headers;
            }
        };
        App.requestQueue.add(getRequst);
    }
    public void putUser(JSONObject data,final UserPOSTCallBack cb){
        String url=Constants.server_url+"/api/userinfos";
        JsonObjectRequest getRequst=new JsonObjectRequest(Request.Method.PUT, url,data, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    cb.ok(response);
                    Log.e("putSucces",response.toString());
                } catch (JSONException e) {
                    e.printStackTrace();
                    Log.e("putFalse",e.toString());
                }
            }
        },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        error.printStackTrace();;
                        cb.error(error.toString());
                        Log.e("putError",error.toString());
                    }
                }){
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String,String> headers=new HashMap<String,String>();
                CookieStore cookieJar=App.cookieManager.getCookieStore();
                List<HttpCookie> cookies=cookieJar.getCookies();
                for(HttpCookie cookie:cookies){
                    if(cookie.toString().contains("XSRF-TOKE")){
                        Log.e("User3-token-post",cookie. getValue());
                        headers.put("X-XSRF-TOKEN",cookie.getValue());
                    }
                }
                headers.put("Content-Type","application/json;charset=utf-8");
                return headers;
            }
        };
        App.requestQueue.add(getRequst);
    }
    public void delUser(int id,final UserPOSTCallBack cb){
        String url=Constants.server_url+"/api/userinfos/{id}"+id;
        JsonObjectRequest getRequst=new JsonObjectRequest(Request.Method.DELETE, url, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    cb.ok(response);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        error.printStackTrace();
                        cb.error(error.toString());
                    }
                }){
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String,String> headers=new HashMap<String,String>();
                CookieStore cookieJar=App.cookieManager.getCookieStore();
                List<HttpCookie> cookies=cookieJar.getCookies();
                for(HttpCookie cookie:cookies){
                    if(cookie.toString().contains("CXRF-TOKE")){
                        Log.e("User4-token-post",cookie.getValue());
                        headers.put("X-XSRF-TOKEN",cookie.getValue());
                    }
                }
                headers.put("Content-Type","application/json;charset=utf-8");
                return headers;
            }
        };
        App.requestQueue.add(getRequst);
    }
    public void getUser(String phone,final UserPOSTCallBack cb){
        String url=Constants.server_url+"/api/userinfos-phonenumber/"+phone;

        JsonObjectRequest delRequest = new JsonObjectRequest(Request.Method.GET, url, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        try {
                            cb.ok(response);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                cb.error(error.toString());
            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> headers = new HashMap<String, String>();
                CookieStore cookieJar = App.cookieManager.getCookieStore();
                List<HttpCookie> cookies = cookieJar.getCookies();
                for (HttpCookie cookie : cookies) {
                    if (cookie.toString().contains("XSRF-TOKE")) {
                        Log.e("User5-token-post", cookie.getValue());
                        headers.put("X-XSRF-TOKEN", cookie.getValue());
                    }
                }

                headers.put("Content-Type", "application/json; charset=utf-8");
                return headers;
            }
        };
        App.requestQueue.add(delRequest);
    }
    public void getAccout(String login,final UserPOSTCallBack cb){
        String url=Constants.server_url+"/api/users/"+login;

        JsonObjectRequest delRequest = new JsonObjectRequest(Request.Method.GET, url, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        try {
                            cb.ok(response);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                cb.error(error.toString());
            }
        }) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                HashMap<String, String> headers = new HashMap<String, String>();
                CookieStore cookieJar = App.cookieManager.getCookieStore();
                List<HttpCookie> cookies = cookieJar.getCookies();
                for (HttpCookie cookie : cookies) {
                    if (cookie.toString().contains("XSRF-TOKE")) {
                        Log.e("User6-token-post", cookie.getValue());
                        headers.put("X-XSRF-TOKEN", cookie.getValue());
                    }
                }

                headers.put("Content-Type", "application/json; charset=utf-8");
                return headers;
            }
        };
        App.requestQueue.add(delRequest);
    }

}
